/** * term.js - an xterm emulator * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License) * https://github.com/chjj/term.js * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * Originally forked from (with the author's permission): * Fabrice Bellard's javascript vt100 for jslinux: * http://bellard.org/jslinux/ * Copyright (c) 2011 Fabrice Bellard * The original design remains. The terminal itself * has been extended to include xterm CSI codes, among * other features. */(function(){"use strict";function n(){this._events=this._events||{}}function r(){n.call(this)}function h(e){var n=this;if(!(this instanceof h))return new h(arguments[0],arguments[1],arguments[2]);r.call(this),typeof e=="number"&&(e={cols:arguments[0],rows:arguments[1],handler:arguments[2]}),e=e||{},T(N(h.defaults),function(t){e[t]==null&&(e[t]=h.options[t],h[t]!==h.defaults[t]&&(e[t]=h[t])),n[t]=e[t]}),e.colors.length===8?e.colors=e.colors.concat(h._colors.slice(8)):e.colors.length===16?e.colors=e.colors.concat(h._colors.slice(16)):e.colors.length===10?e.colors=e.colors.slice(0,-2).concat(h._colors.slice(8,-2),e.colors.slice(-2)):e.colors.length===18&&(e.colors=e.colors.slice(0,-2).concat(h._colors.slice(16,-2),e.colors.slice(-2))),this.colors=e.colors,this.options=e,this.parent=e.body||e.parent||(t?t.getElementsByTagName("body")[0]:null),this.cols=e.cols||e.geometry[0],this.rows=e.rows||e.geometry[1],this.setRawMode,this.isTTY=!0,this.isRaw=!0,this.columns=this.cols,this.rows=this.rows,e.handler&&this.on("data",e.handler),this.ybase=0,this.ydisp=0,this.x=0,this.y=0,this.cursorState=0,this.cursorHidden=!1,this.convertEol,this.state=0,this.queue="",this.scrollTop=0,this.scrollBottom=this.rows-1,this.applicationKeypad=!1,this.applicationCursor=!1,this.originMode=!1,this.insertMode=!1,this.wraparoundMode=!1,this.normal=null,this.prefixMode=!1,this.selectMode=!1,this.visualMode=!1,this.searchMode=!1,this.searchDown,this.entry="",this.entryPrefix="Search: ",this._real,this._selected,this._textarea,this.charset=null,this.gcharset=null,this.glevel=0,this.charsets=[null],this.decLocator,this.x10Mouse,this.vt200Mouse,this.vt300Mouse,this.normalMouse,this.mouseEvents,this.sendFocus,this.utfMouse,this.sgrMouse,this.urxvtMouse,this.element,this.children,this.refreshStart,this.refreshEnd,this.savedX,this.savedY,this.savedCols,this.readable=!0,this.writable=!0,this.defAttr=131840,this.curAttr=this.defAttr,this.params=[],this.currentParam=0,this.prefix="",this.postfix="",this.lines=[];var i=this.rows;while(i--)this.lines.push(this.blankLine());this.tabs,this.setupStops()}function p(e,t,n,r){e.addEventListener(t,n,r||!1)}function d(e,t,n,r){e.removeEventListener(t,n,r||!1)}function v(e){return e.preventDefault&&e.preventDefault(),e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble=!0,!1}function m(e,t){function n(){this.constructor=e}n.prototype=t.prototype,e.prototype=new n}function g(e){var t=e.getElementsByTagName("body")[0],n=e.createElement("div");n.className="terminal";var r=e.createElement("div"),i=e.createElement("span");i.innerHTML="hello world",r.appendChild(i),n.appendChild(r),t.appendChild(n);var s=i.scrollWidth;i.style.fontWeight="bold";var o=i.scrollWidth;return t.removeChild(n),s!==o}function E(e,t){var n=e.length;while(n--)if(e[n]===t)return n;return-1}function S(e){return e<="＀"?!1:e>="!"&&e<="ᄒ"||e>="ᅡ"&&e<="ᅦ"||e>="ᅧ"&&e<="ᅬ"||e>="ᅭ"&&e<="ᅲ"||e>="ᅳ"&&e<="ᅵ"||e>="¢"&&e<="₩"||e>="│"&&e<="○"}function x(e,t,n){var r=e<<16|t<<8|n;if(x._cache[r]!=null)return x._cache[r];var i=Infinity,s=-1,o=0,u,a,f,l,c;for(;o>16&255,r>>8&255,r&255]);return e}(),h.defaults={colors:h.colors,convertEol:!1,termName:"xterm",geometry:[80,24],cursorBlink:!0,visualBell:!1,popOnBell:!1,scrollback:1e3,screenKeys:!1,debug:!1,useStyle:!1},h.options={},T(N(h.defaults),function(e){h[e]=h.defaults[e],h.options[e]=h.defaults[e]}),h.focus=null,h.prototype.focus=function(){if(h.focus===this)return;h.focus&&h.focus.blur(),this.sendFocus&&this.send(""),this.showCursor(),h.focus=this},h.prototype.blur=function(){if(h.focus!==this)return;this.cursorState=0,this.refresh(this.y,this.y),this.sendFocus&&this.send(""),h.focus=null},h.prototype.initGlobal=function(){var e=this.document;h._boundDocs=h._boundDocs||[];if(~E(h._boundDocs,e))return;h._boundDocs.push(e),h.bindPaste(e),h.bindKeys(e),h.bindCopy(e),this.isMobile&&this.fixMobile(e),this.useStyle&&h.insertStyle(e,this.colors[256],this.colors[257])},h.bindPaste=function(e){var t=e.defaultView;p(t,"paste",function(e){var t=h.focus;if(!t)return;return e.clipboardData?t.send(e.clipboardData.getData("text/plain")):t.context.clipboardData&&t.send(t.context.clipboardData.getData("Text")),t.element.contentEditable="inherit",v(e)})},h.bindKeys=function(e){p(e,"keydown",function(e){if(!h.focus)return;var t=e.target||e.srcElement;if(!t)return;if(t===h.focus.element||t===h.focus.context||t===h.focus.document||t===h.focus.body||t===h._textarea||t===h.focus.parent)return h.focus.keyDown(e)},!0),p(e,"keypress",function(e){if(!h.focus)return;var t=e.target||e.srcElement;if(!t)return;if(e.ctrlKey&&e.key==="v")return;if(t===h.focus.element||t===h.focus.context||t===h.focus.document||t===h.focus.body||t===h._textarea||t===h.focus.parent)return h.focus.element.contentEditable="inherit",h.focus.keyPress(e)},!0),p(e,"mousedown",function(e){if(!h.focus)return;var t=e.target||e.srcElement;if(!t)return;do if(t===h.focus.element)return;while(t=t.parentNode);h.focus.blur()})},h.bindCopy=function(e){var t=e.defaultView;p(t,"copy",function(e){var t=h.focus;if(!t)return;if(!t._selected)return;var n=t.getCopyTextarea(),r=t.grabText(t._selected.x1,t._selected.x2,t._selected.y1,t._selected.y2);t.emit("copy",r),n.focus(),n.textContent=r,n.value=r,n.setSelectionRange(0,r.length),b(function(){t.element.focus(),t.focus()},1)})},h.prototype.fixMobile=function(e){var t=this,n=e.createElement("textarea");n.style.position="absolute",n.style.left="-32000px",n.style.top="-32000px",n.style.width="0px",n.style.height="0px",n.style.opacity="0",n.style.backgroundColor="transparent",n.style.borderStyle="none",n.style.outlineStyle="none",n.autocapitalize="none",n.autocorrect="off",e.getElementsByTagName("body")[0].appendChild(n),h._textarea=n,b(function(){n.focus()},1e3),this.isAndroid&&p(n,"change",function(){var e=n.textContent||n.value;n.value="",n.textContent="",t.send(e+"\r")})},h.insertStyle=function(e,t,n){var r=e.getElementById("term-style");if(r)return;var i=e.getElementsByTagName("head")[0];if(!i)return;var r=e.createElement("style");r.id="term-style",r.innerHTML=".terminal {\n float: left;\n border: "+t+" solid 5px;\n"+' font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;\n'+" font-size: 11px;\n"+" color: "+n+";\n"+" background: "+t+";\n"+"}\n"+"\n"+".terminal-cursor {\n"+" color: "+t+";\n"+" background: "+n+";\n"+"}\n",i.insertBefore(r,i.firstChild)},h.prototype.open=function(t){var n=this,r=0,i;this.parent=t||this.parent;if(!this.parent)throw new Error("Terminal requires a parent element.");this.context=this.parent.ownerDocument.defaultView,this.document=this.parent.ownerDocument,this.body=this.document.getElementsByTagName("body")[0],this.context.navigator&&this.context.navigator.userAgent&&(this.isMac=!!~this.context.navigator.userAgent.indexOf("Mac"),this.isIpad=!!~this.context.navigator.userAgent.indexOf("iPad"),this.isIphone=!!~this.context.navigator.userAgent.indexOf("iPhone"),this.isAndroid=!!~this.context.navigator.userAgent.indexOf("Android"),this.isMobile=this.isIpad||this.isIphone||this.isAndroid,this.isMSIE=!!~this.context.navigator.userAgent.indexOf("MSIE"),this.isFirefox=!!~this.context.navigator.userAgent.indexOf("Firefox")),this.element=this.document.createElement("div"),this.element.className="terminal",this.element.style.outline="none",this.element.setAttribute("tabindex",0),this.element.setAttribute("spellcheck","false"),this.element.style.backgroundColor=this.colors[256],this.element.style.color=this.colors[257],this.children=[];for(;r127&&(n=127),e.push(n)}else{if(n===2047)return e.push(0);n<127?e.push(n):(n>2047&&(n=2047),e.push(192|n>>6),e.push(128|n&63))}}function u(e,n){if(t.vt300Mouse){e&=3,n.x-=32,n.y-=32;var r="[24";if(e===0)r+="1";else if(e===1)r+="3";else if(e===2)r+="5";else{if(e===3)return;r+="0"}r+="~["+n.x+","+n.y+"]\r",t.send(r);return}if(t.decLocator){e&=3,n.x-=32,n.y-=32,e===0?e=2:e===1?e=4:e===2?e=6:e===3&&(e=3),t.send("["+e+";"+(e===3?4:0)+";"+n.y+";"+n.x+";"+(n.page||0)+"&w");return}if(t.urxvtMouse){n.x-=32,n.y-=32,n.x++,n.y++,t.send("["+e+";"+n.x+";"+n.y+"M");return}if(t.sgrMouse){n.x-=32,n.y-=32,t.send("[<"+((e&3)===3?e&-4:e)+";"+n.x+";"+n.y+((e&3)===3?"m":"M"));return}var r=[];o(r,e),o(r,n.x),o(r,n.y),t.send(""+y.fromCharCode.apply(y,r))}function a(e){var n,r,i,s,o;switch(e.type){case"mousedown":n=e.button!=null?+e.button:e.which!=null?e.which-1:null,t.isMSIE&&(n=n===1?0:n===4?1:n);break;case"mouseup":n=3;break;case"DOMMouseScroll":n=e.detail<0?64:65;break;case"mousewheel":n=e.wheelDeltaY>0?64:65}return r=e.shiftKey?4:0,i=e.metaKey?8:0,s=e.ctrlKey?16:0,o=r|i|s,t.vt200Mouse?o&=s:t.normalMouse||(o=0),n=32+(o<<2)+n,n}function f(e){var n,i,s,o,u;if(e.pageX==null)return;n=e.pageX,i=e.pageY,u=t.element;while(u&&u!==t.document.documentElement)n-=u.offsetLeft,i-=u.offsetTop,u="offsetParent"in u?u.offsetParent:u.parentNode;return s=t.element.clientWidth,o=t.element.clientHeight,n=Math.round(n/s*t.cols),i=Math.round(i/o*t.rows),n<0&&(n=0),n>t.cols&&(n=t.cols),i<0&&(i=0),i>t.rows&&(i=t.rows),n+=32,i+=32,{x:n,y:i,type:e.type===r?"mousewheel":e.type}}var e=this.element,t=this,n=32,r="onmousewheel"in this.context?"mousewheel":"DOMMouseScroll";p(e,"mousedown",function(e){if(!t.mouseEvents)return;return i(e),t.focus(),t.normalMouse&&p(t.document,"mousemove",s),t.x10Mouse||p(t.document,"mouseup",function n(e){return i(e),t.normalMouse&&d(t.document,"mousemove",s),d(t.document,"mouseup",n),v(e)}),v(e)}),p(e,r,function(e){if(!t.mouseEvents)return;if(t.x10Mouse||t.vt300Mouse||t.decLocator)return;return i(e),v(e)}),p(e,r,function(e){if(t.mouseEvents)return;if(t.applicationKeypad)return;return e.type==="DOMMouseScroll"?t.scrollDisp(e.detail<0?-5:5):t.scrollDisp(e.wheelDeltaY>0?-5:5),v(e)})},h.prototype.close=h.prototype.destroySoon=h.prototype.destroy=function(){if(this.destroyed)return;this._blink&&(clearInterval(this._blink),delete this._blink),this.readable=!1,this.writable=!1,this.destroyed=!0,this._events={},this.handler=function(){},this.write=function(){},this.end=function(){},this.element.parentNode&&this.element.parentNode.removeChild(this.element),this.emit("end"),this.emit("close"),this.emit("finish"),this.emit("destroy")},h.prototype.refresh=function(e,t){var n,r,i,s,o,u,a,f,l,c,p,d,v,m;t-e>=this.rows/2&&(m=this.element.parentNode,m&&m.removeChild(this.element)),a=this.cols,r=e,t>=this.lines.length&&(this.log("`end` is too large. Most likely a bad CSR."),t=this.lines.length-1);for(;r<=t;r++){v=r+this.ydisp,s=this.lines[v],o="",r===this.y&&this.cursorState&&(this.ydisp===this.ybase||this.selectMode)&&!this.cursorHidden?n=this.x:n=-1,l=this.defAttr,i=0;for(;i"),f!==this.defAttr&&(f===-1?o+='':(o+='>9&511,p=f&511,d&1&&p<8&&(p+=8)),d&16&&(o+="visibility:hidden;"),c!==256&&(o+="background-color:"+this.colors[c]+";"),p!==257&&(o+="color:"+this.colors[p]+";"),o+='">')));switch(u){case"&":o+="&";break;case"<":o+="<";break;case">":o+=">";break;default:u<=" "?o+=" ":(S(u)&&i++,o+=u)}l=f}l!==this.defAttr&&(o+=""),this.children[r].innerHTML=o}m&&m.appendChild(this.element)},h.prototype._cursorBlink=function(){if(h.focus!==this)return;this.cursorState^=1,this.refresh(this.y,this.y)},h.prototype.showCursor=function(){this.cursorState||(this.cursorState=1,this.refresh(this.y,this.y))},h.prototype.startBlink=function(){if(!this.cursorBlink)return;var e=this;this._blinker=function(){e._cursorBlink()},this._blink=w(this._blinker,500)},h.prototype.refreshBlink=function(){if(!this.cursorBlink||!this._blink)return;clearInterval(this._blink),this._blink=w(this._blinker,500)},h.prototype.scroll=function(){var e;++this.ybase===this.scrollback&&(this.ybase=this.ybase/2|0,this.lines=this.lines.slice(-(this.ybase+this.rows)+1)),this.ydisp=this.ybase,e=this.ybase+this.rows-1,e-=this.rows-1-this.scrollBottom,e===this.lines.length?this.lines.push(this.blankLine()):this.lines.splice(e,0,this.blankLine()),this.scrollTop!==0&&(this.ybase!==0&&(this.ybase--,this.ydisp=this.ybase),this.lines.splice(this.ybase+this.scrollTop,1)),this.updateRange(this.scrollTop),this.updateRange(this.scrollBottom)},h.prototype.scrollDisp=function(e){this.ydisp+=e,this.ydisp>this.ybase?this.ydisp=this.ybase:this.ydisp<0&&(this.ydisp=0),this.refresh(0,this.rows-1)},h.prototype.write=function(e){var t=e.length,n=0,r,p,d;this.refreshStart=this.y,this.refreshEnd=this.y,this.ybase!==this.ydisp&&(this.ydisp=this.ybase,this.maxRange());for(;nthis.scrollBottom&&(this.y--,this.scroll());break;case"\r":this.x=0;break;case"\b":this.x>0&&this.x--;break;case" ":this.x=this.nextStop();break;case"":this.setgLevel(1);break;case"":this.setgLevel(0);break;case"":this.state=s;break;default:if(d>=" "){this.charset&&this.charset[d]&&(d=this.charset[d]),this.x>=this.cols&&(this.x=0,this.y++,this.y>this.scrollBottom&&(this.y--,this.scroll())),this.lines[this.y+this.ybase][this.x]=[this.curAttr,d],this.x++,this.updateRange(this.y);if(S(d)){r=this.y+this.ybase;if(this.cols<2||this.x>=this.cols){this.lines[r][this.x-1]=[this.curAttr," "];break}this.lines[r][this.x]=[this.curAttr," "],this.x++}}}break;case s:switch(d){case"[":this.params=[],this.currentParam=0,this.state=o;break;case"]":this.params=[],this.currentParam=0,this.state=u;break;case"P":this.params=[],this.prefix="",this.currentParam="",this.state=f;break;case"_":this.state=l;break;case"^":this.state=l;break;case"c":this.reset();break;case"E":this.x=0;case"D":this.index();break;case"M":this.reverseIndex();break;case"%":this.setgLevel(0),this.setgCharset(0,h.charsets.US),this.state=i,n++;break;case"(":case")":case"*":case"+":case"-":case".":switch(d){case"(":this.gcharset=0;break;case")":this.gcharset=1;break;case"*":this.gcharset=2;break;case"+":this.gcharset=3;break;case"-":this.gcharset=1;break;case".":this.gcharset=2}this.state=a;break;case"/":this.gcharset=3,this.state=a,n--;break;case"N":break;case"O":break;case"n":this.setgLevel(2);break;case"o":this.setgLevel(3);break;case"|":this.setgLevel(3);break;case"}":this.setgLevel(2);break;case"~":this.setgLevel(1);break;case"7":this.saveCursor(),this.state=i;break;case"8":this.restoreCursor(),this.state=i;break;case"#":this.state=i,n++;break;case"H":this.tabSet();break;case"=":this.log("Serial port requested application keypad."),this.applicationKeypad=!0,this.state=i;break;case">":this.log("Switching back to normal keypad."),this.applicationKeypad=!1,this.state=i;break;default:this.state=i,this.error("Unknown ESC control: %s.",d)}break;case a:switch(d){case"0":p=h.charsets.SCLD;break;case"A":p=h.charsets.UK;break;case"B":p=h.charsets.US;break;case"4":p=h.charsets.Dutch;break;case"C":case"5":p=h.charsets.Finnish;break;case"R":p=h.charsets.French;break;case"Q":p=h.charsets.FrenchCanadian;break;case"K":p=h.charsets.German;break;case"Y":p=h.charsets.Italian;break;case"E":case"6":p=h.charsets.NorwegianDanish;break;case"Z":p=h.charsets.Spanish;break;case"H":case"7":p=h.charsets.Swedish;break;case"=":p=h.charsets.Swiss;break;case"/":p=h.charsets.ISOLatin,n++;break;default:p=h.charsets.US}this.setgCharset(this.gcharset,p),this.gcharset=null,this.state=i;break;case u:if(this.lch===""&&d==="\\"||d===""){this.lch===""&&(typeof this.currentParam=="string"?this.currentParam=this.currentParam.slice(0,-1):typeof this.currentParam=="number"&&(this.currentParam=(this.currentParam-("".charCodeAt(0)-48))/10)),this.params.push(this.currentParam);switch(this.params[0]){case 0:case 1:case 2:this.params[1]&&(this.title=this.params[1],this.handleTitle(this.title));break;case 3:break;case 4:case 5:break;case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:break;case 46:break;case 50:break;case 51:break;case 52:break;case 104:case 105:case 110:case 111:case 112:case 113:case 114:case 115:case 116:case 117:case 118:}this.params=[],this.currentParam=0,this.state=i}else this.params.length?this.currentParam+=d:d>="0"&&d<="9"?this.currentParam=this.currentParam*10+d.charCodeAt(0)-48:d===";"&&(this.params.push(this.currentParam),this.currentParam="");break;case o:if(d==="?"||d===">"||d==="!"){this.prefix=d;break}if(d>="0"&&d<="9"){this.currentParam=this.currentParam*10+d.charCodeAt(0)-48;break}if(d==="$"||d==='"'||d===" "||d==="'"){this.postfix=d;break}this.params.push(this.currentParam),this.currentParam=0;if(d===";")break;this.state=i;switch(d){case"A":this.cursorUp(this.params);break;case"B":this.cursorDown(this.params);break;case"C":this.cursorForward(this.params);break;case"D":this.cursorBackward(this.params);break;case"H":this.cursorPos(this.params);break;case"J":this.eraseInDisplay(this.params);break;case"K":this.eraseInLine(this.params);break;case"m":this.prefix||this.charAttributes(this.params);break;case"n":this.prefix||this.deviceStatus(this.params);break;case"@":this.insertChars(this.params);break;case"E":this.cursorNextLine(this.params);break;case"F":this.cursorPrecedingLine(this.params);break;case"G":this.cursorCharAbsolute(this.params);break;case"L":this.insertLines(this.params);break;case"M":this.deleteLines(this.params);break;case"P":this.deleteChars(this.params);break;case"X":this.eraseChars(this.params);break;case"`":this.charPosAbsolute(this.params);break;case"a":this.HPositionRelative(this.params);break;case"c":this.sendDeviceAttributes(this.params);break;case"d":this.linePosAbsolute(this.params);break;case"e":this.VPositionRelative(this.params);break;case"f":this.HVPosition(this.params);break;case"h":this.setMode(this.params);break;case"l":this.resetMode(this.params);break;case"r":this.setScrollRegion(this.params);break;case"s":this.saveCursor(this.params);break;case"u":this.restoreCursor(this.params);break;case"I":this.cursorForwardTab(this.params);break;case"S":this.scrollUp(this.params);break;case"T":this.params.length<2&&!this.prefix&&this.scrollDown(this.params);break;case"Z":this.cursorBackwardTab(this.params);break;case"b":this.repeatPrecedingCharacter(this.params);break;case"g":this.tabClear(this.params);break;case"p":switch(this.prefix){case"!":this.softReset(this.params)}break;default:this.error("Unknown CSI code: %s.",d)}this.prefix="",this.postfix="";break;case f:if(this.lch===""&&d==="\\"||d===""){if(this.prefix==="tmux;"&&d===""){this.currentParam+=d;continue}this.lch===""&&(typeof this.currentParam=="string"?this.currentParam=this.currentParam.slice(0,-1):typeof this.currentParam=="number"&&(this.currentParam=(this.currentParam-("".charCodeAt(0)-48))/10)),this.params.push(this.currentParam);var v=this.params[this.params.length-1];switch(this.prefix){case c:this.emit("udk",{clearAll:this.params[0]===0,eraseBelow:this.params[0]===1,lockKeys:this.params[1]===0,dontLockKeys:this.params[1]===1,keyList:(this.params[2]+"").split(";").map(function(e){return e=e.split("/"),{keyCode:e[0],hexKeyValue:e[1]}})});break;case"$q":var m=0;switch(v){case'"q':v='0"q',m=1;break;case'"p':v='61;0"p',m=1;break;case"r":v=""+(this.scrollTop+1)+";"+(this.scrollBottom+1)+"r",m=1;break;case"m":m=0;break;default:this.error("Unknown DCS Pt: %s.",v),m=0}this.send("P"+m+"$r"+v+"\\");break;case"+p":this.emit("set terminfo",{name:this.params[0]});break;case"+q":var m=!1;this.send("P"+ +m+"+r"+v+"\\");break;case"tmux;":this.emit("passthrough",v);break;default:this.error("Unknown DCS prefix: %s.",v)}this.currentParam=0,this.prefix="",this.state=i}else{this.currentParam+=d;if(!this.prefix)if(/^\d*;\d*\|/.test(this.currentParam))this.prefix=c,this.params=this.currentParam.split(/[;|]/).map(function(e){return e.length?+e:0}).slice(0,-1),this.currentParam="";else if(/^[$+][a-zA-Z]/.test(this.currentParam)||/^\w+;\x1b/.test(this.currentParam))this.prefix=this.currentParam,this.currentParam=""}break;case l:if(this.lch===""&&d==="\\"||d==="")this.state=i}}return this.updateRange(this.y),this.refresh(this.refreshStart,this.refreshEnd),!0},h.prototype.writeln=function(e){return this.write(e+"\r\n")},h.prototype.end=function(e){var t=!0;return e&&(t=this.write(e)),this.destroySoon(),t},h.prototype.resume=function(){},h.prototype.pause=function(){},h.prototype.keyDown=function(e){var t=this,n;switch(e.keyCode){case 8:if(e.altKey){n="";break}if(e.shiftKey){n="\b";break}n="";break;case 9:if(e.shiftKey){n="";break}n=" ";break;case 13:n="\r";break;case 27:n="";break;case 32:n=" ";break;case 37:if(this.applicationCursor){n="OD";break}if(e.ctrlKey){n="";break}n="";break;case 39:if(this.applicationCursor){n="OC";break}if(e.ctrlKey){n="";break}n="";break;case 38:if(this.applicationCursor){n="OA";break}if(e.ctrlKey)return this.scrollDisp(-1),v(e);n="";break;case 40:if(this.applicationCursor){n="OB";break}if(e.ctrlKey)return this.scrollDisp(1),v(e);n="";break;case 46:n="[3~";break;case 45:n="[2~";break;case 36:if(this.applicationKeypad){n="OH";break}n="OH";break;case 35:if(this.applicationKeypad){n="OF";break}n="OF";break;case 33:if(e.shiftKey)return this.scrollDisp(-(this.rows-1)),v(e);n="[5~";break;case 34:if(e.shiftKey)return this.scrollDisp(this.rows-1),v(e);n="[6~";break;case 112:n="OP";break;case 113:n="OQ";break;case 114:n="OR";break;case 115:n="OS";break;case 116:n="[15~";break;case 117:n="[17~";break;case 118:n="[18~";break;case 119:n="[19~";break;case 120:n="[20~";break;case 121:n="[21~";break;case 122:n="[23~";break;case 123:n="[24~";break;default:if(e.ctrlKey)if(e.keyCode>=65&&e.keyCode<=90){if(this.screenKeys&&!this.prefixMode&&!this.selectMode&&e.keyCode===65)return this.enterPrefix(),v(e);if(this.prefixMode&&e.keyCode===86){this.leavePrefix();var r=h.focus;r.element.contentEditable="true";return}if((this.prefixMode||this.selectMode)&&e.keyCode===67){this.visualMode&&b(function(){t.leaveVisual()},1);return}n=y.fromCharCode(e.keyCode-64)}else e.keyCode===32?n=y.fromCharCode(0):e.keyCode>=51&&e.keyCode<=55?n=y.fromCharCode(e.keyCode-51+27):e.keyCode===56?n=y.fromCharCode(127):e.keyCode===219?n=y.fromCharCode(27):e.keyCode===221&&(n=y.fromCharCode(29));else e.altKey&&(e.keyCode>=65&&e.keyCode<=90?n=""+y.fromCharCode(e.keyCode+32):e.keyCode===192?n="`":e.keyCode>=48&&e.keyCode<=57&&(n=""+(e.keyCode-48)))}return n?this.prefixMode?(this.leavePrefix(),v(e)):this.selectMode?(this.keySelect(e,n),v(e)):(this.emit("keydown",e),this.emit("key",n,e),this.showCursor(),this.handler(n),v(e)):!0},h.prototype.setgLevel=function(e){this.glevel=e,this.charset=this.charsets[e]},h.prototype.setgCharset=function(e,t){this.charsets[e]=t,this.glevel===e&&(this.charset=t)},h.prototype.keyPress=function(e){var t;v(e);if(e.charCode)t=e.charCode;else if(e.which==null)t=e.keyCode;else{if(e.which===0||e.charCode===0)return!1;t=e.which}return!t||e.ctrlKey||e.altKey||e.metaKey?!1:(t=y.fromCharCode(t),this.prefixMode?(this.leavePrefix(),this.keyPrefix(e,t),!1):this.selectMode?(this.keySelect(e,t),!1):(this.emit("keypress",t,e),this.emit("key",t,e),this.showCursor(),this.handler(t),!1))},h.prototype.send=function(e){var t=this;this.queue||b(function(){t.handler(t.queue),t.queue=""},1),this.queue+=e},h.prototype.bell=function(){this.emit("bell");if(!this.visualBell)return;var e=this;this.element.style.borderColor="white",b(function(){e.element.style.borderColor=""},10),this.popOnBell&&this.focus()},h.prototype.log=function(){if(!this.debug)return;if(!this.context.console||!this.context.console.log)return;var e=Array.prototype.slice.call(arguments);this.context.console.log.apply(this.context.console,e)},h.prototype.error=function(){if(!this.debug)return;if(!this.context.console||!this.context.console.error)return;var e=Array.prototype.slice.call(arguments);this.context.console.error.apply(this.context.console,e)},h.prototype.resize=function(e,t){var n,r,i,s,o;e<1&&(e=1),t<1&&(t=1),s=this.cols;if(se){i=this.lines.length;while(i--)while(this.lines[i].length>e)this.lines[i].pop()}this.setupStops(s),this.cols=e,this.columns=e,s=this.rows;if(st)while(s-->t){this.lines.length>t+this.ybase&&this.lines.pop();if(this.children.length>t){r=this.children.pop();if(!r)continue;r.parentNode.removeChild(r)}}this.rows=t,this.y>=t&&(this.y=t-1),this.x>=e&&(this.x=e-1),this.scrollTop=0,this.scrollBottom=t-1,this.refresh(0,this.rows-1),this.normal=null,this.emit("resize")},h.prototype.updateRange=function(e){ethis.refreshEnd&&(this.refreshEnd=e)},h.prototype.maxRange=function(){this.refreshStart=0,this.refreshEnd=this.rows-1},h.prototype.setupStops=function(e){e!=null?this.tabs[e]||(e=this.prevStop(e)):(this.tabs={},e=0);for(;e0);return e>=this.cols?this.cols-1:e<0?0:e},h.prototype.nextStop=function(e){e==null&&(e=this.x);while(!this.tabs[++e]&&e=this.cols?this.cols-1:e<0?0:e},h.prototype.eraseAttr=function(){return this.defAttr&-512|this.curAttr&511},h.prototype.eraseRight=function(e,t){var n=this.lines[this.ybase+t],r=[this.eraseAttr()," "];for(;ethis.scrollBottom&&(this.y--,this.scroll()),this.state=i},h.prototype.reverseIndex=function(){var e;this.y--,this.y=this.rows&&(this.y=this.rows-1)},h.prototype.cursorForward=function(e){var t=e[0];t<1&&(t=1),this.x+=t,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.cursorBackward=function(e){var t=e[0];t<1&&(t=1),this.x-=t,this.x<0&&(this.x=0)},h.prototype.cursorPos=function(e){var t,n;t=e[0]-1,e.length>=2? n=e[1]-1:n=0,t<0?t=0:t>=this.rows&&(t=this.rows-1),n<0?n=0:n>=this.cols&&(n=this.cols-1),this.x=n,this.y=t},h.prototype.eraseInDisplay=function(e){var t;switch(e[0]){case 0:this.eraseRight(this.x,this.y),t=this.y+1;for(;t>18,i=this.curAttr>>9&511,s=this.curAttr&511,o;for(;n=30&&o<=37?i=o-30:o>=40&&o<=47?s=o-40:o>=90&&o<=97?(o+=8,i=o-90):o>=100&&o<=107?(o+=8,s=o-100):o===0?(r=this.defAttr>>18,i=this.defAttr>>9&511,s=this.defAttr&511):o===1?r|=1:o===4?r|=2:o===5?r|=4:o===7?r|=8:o===8?r|=16:o===22?r&=-2:o===24?r&=-3:o===25?r&=-5:o===27?r&=-9:o===28?r&=-17:o===39?i=this.defAttr>>9&511:o===49?s=this.defAttr&511:o===38?e[n+1]===2?(n+=2,i=x(e[n]&255,e[n+1]&255,e[n+2]&255),i===-1&&(i=511),n+=2):e[n+1]===5&&(n+=2,o=e[n]&255,i=o):o===48?e[n+1]===2?(n+=2,s=x(e[n]&255,e[n+1]&255,e[n+2]&255),s===-1&&(s=511),n+=2):e[n+1]===5&&(n+=2,o=e[n]&255,s=o):o===100?(i=this.defAttr>>9&511,s=this.defAttr&511):this.error("Unknown SGR attribute: %d.",o);this.curAttr=r<<18|i<<9|s},h.prototype.deviceStatus=function(e){if(!this.prefix)switch(e[0]){case 5:this.send("");break;case 6:this.send("["+(this.y+1)+";"+(this.x+1)+"R")}else if(this.prefix==="?")switch(e[0]){case 6:this.send("[?"+(this.y+1)+";"+(this.x+1)+"R");break;case 15:break;case 25:break;case 26:break;case 53:}},h.prototype.insertChars=function(e){var t,n,r,i;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.x,i=[this.eraseAttr()," "];while(t--&&r=this.rows&&(this.y=this.rows-1),this.x=0},h.prototype.cursorPrecedingLine=function(e){var t=e[0];t<1&&(t=1),this.y-=t,this.y<0&&(this.y=0),this.x=0},h.prototype.cursorCharAbsolute=function(e){var t=e[0];t<1&&(t=1),this.x=t-1},h.prototype.insertLines=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.rows-1-this.scrollBottom,r=this.rows-1+this.ybase-r+1;while(t--)this.lines.splice(n,0,this.blankLine(!0)),this.lines.splice(r,1);this.updateRange(this.y),this.updateRange(this.scrollBottom)},h.prototype.deleteLines=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.rows-1-this.scrollBottom,r=this.rows-1+this.ybase-r;while(t--)this.lines.splice(r+1,0,this.blankLine(!0)),this.lines.splice(n,1);this.updateRange(this.y),this.updateRange(this.scrollBottom)},h.prototype.deleteChars=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=[this.eraseAttr()," "];while(t--)this.lines[n].splice(this.x,1),this.lines[n].push(r)},h.prototype.eraseChars=function(e){var t,n,r,i;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.x,i=[this.eraseAttr()," "];while(t--&&r=this.cols&&(this.x=this.cols-1)},h.prototype.HPositionRelative=function(e){var t=e[0];t<1&&(t=1),this.x+=t,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.sendDeviceAttributes=function(e){if(e[0]>0)return;this.prefix?this.prefix===">"&&(this.is("xterm")?this.send("[>0;276;0c"):this.is("rxvt-unicode")?this.send("[>85;95;0c"):this.is("linux")?this.send(e[0]+"c"):this.is("screen")&&this.send("[>83;40003;0c")):this.is("xterm")||this.is("rxvt-unicode")||this.is("screen")?this.send("[?1;2c"):this.is("linux")&&this.send("[?6c")},h.prototype.linePosAbsolute=function(e){var t=e[0];t<1&&(t=1),this.y=t-1,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.VPositionRelative=function(e){var t=e[0];t<1&&(t=1),this.y+=t,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.HVPosition=function(e){e[0]<1&&(e[0]=1),e[1]<1&&(e[1]=1),this.y=e[0]-1,this.y>=this.rows&&(this.y=this.rows-1),this.x=e[1]-1,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.setMode=function(e){if(typeof e=="object"){var t=e.length,n=0;for(;n1e3,this.mouseEvents=!0,this.element.style.cursor="default",this.log("Binding to mouse events.");break;case 1004:this.sendFocus=!0;break;case 1005:this.utfMouse=!0;break;case 1006:this.sgrMouse=!0;break;case 1015:this.urxvtMouse=!0;break;case 25:this.cursorHidden=!1;break;case 1049:case 47:case 1047:if(!this.normal){var r={lines:this.lines,ybase:this.ybase,ydisp:this.ydisp,x:this.x,y:this.y,scrollTop:this.scrollTop,scrollBottom:this.scrollBottom,tabs:this.tabs};this.reset(),this.normal=r,this.showCursor()}}},h.prototype.resetMode=function(e){if(typeof e=="object"){var t=e.length,n=0;for(;n0},h.prototype.eraseRectangle=function(e){var t=e[0],n=e[1],r=e[2],i=e[3],s,o,u;u=[this.eraseAttr()," "];for(;t="0"&&t<="9"?(t=+t-1,~t||(t=9),this.emit("request term",t)):t==="n"?this.emit("request term next"):t==="P"?this.emit("request term previous"):t===":"?this.emit("request command mode"):t==="["&&this.enterSelect()},h.prototype.keySelect=function(e,t){this.showCursor();if(this.searchMode||t==="n"||t==="N")return this.keySearch(e,t);if(t===""){var n=this.ydisp+this.y;this.ydisp===this.ybase?(this.y=Math.min(this.y+(this.rows-1)/2|0,this.rows-1),this.refresh(0,this.rows-1)):this.scrollDisp((this.rows-1)/2|0),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t===""){var n=this.ydisp+this.y;this.ydisp===0?(this.y=Math.max(this.y-(this.rows-1)/2|0,0),this.refresh(0,this.rows-1)):this.scrollDisp(-(this.rows-1)/2|0),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t===""){var n=this.ydisp+this.y;this.scrollDisp(this.rows-1),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t===""){var n=this.ydisp+this.y;this.scrollDisp(-(this.rows-1)),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t==="k"||t===""){var n=this.ydisp+this.y;this.y--,this.y<0&&(this.y=0,this.scrollDisp(-1)),this.visualMode?this.selectText(this.x,this.x,n,this.ydisp+this.y):this.refresh(this.y,this.y+1);return}if(t==="j"||t===""){var n=this.ydisp+this.y;this.y++,this.y>=this.rows&&(this.y=this.rows-1,this.scrollDisp(1)),this.visualMode?this.selectText(this.x,this.x,n,this.ydisp+this.y):this.refresh(this.y-1,this.y);return}if(t==="h"||t===""){var r=this.x;this.x--,this.x<0&&(this.x=0),this.visualMode?this.selectText(r,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="l"||t===""){var r=this.x;this.x++,this.x>=this.cols&&(this.x=this.cols-1),this.visualMode?this.selectText(r,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="v"||t===" "){this.visualMode?this.leaveVisual():this.enterVisual();return}if(t==="y"){if(this.visualMode){var i=this.grabText(this._selected.x1,this._selected.x2,this._selected.y1,this._selected.y2);this.copyText(i),this.leaveVisual()}return}if(t==="q"||t===""){this.visualMode?this.leaveVisual():this.leaveSelect();return}if(t==="w"||t==="W"){var s=this.x,o=this.y,u=this.ydisp,r=this.x,n=this.y,a=this.ydisp,f=!1;for(;;){var l=this.lines[a+n];while(r=this.cols&&(r=this.cols-1);if(r===this.cols-1&&l[r][1]<=" "){r=0;if(++n>=this.rows){n--;if(++a>this.ybase){a=this.ybase,r=this.x;break}}continue}break}this.x=r,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="b"||t==="B"){var s=this.x,o=this.y,u=this.ydisp,r=this.x,n=this.y,a=this.ydisp;for(;;){var l=this.lines[a+n],f=r>0&&l[r][1]>" "&&l[r-1][1]>" ";while(r>=0){if(l[r][1]<=" "){if(f&&r+1" "){r++;break}f=!0}r--}r<0&&(r=0);if(r===0&&(l[r][1]<=" "||!f)){r=this.cols-1;if(--n<0){n++;if(--a<0){a++,r=0;break}}continue}break}this.x=r,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="e"||t==="E"){var r=this.x+1,n=this.y,a=this.ydisp;r>=this.cols&&r--;for(;;){var l=this.lines[a+n];while(r=0&&l[r-1][1]>" "){r--;break}r++}r>=this.cols&&(r=this.cols-1);if(r===this.cols-1&&l[r][1]<=" "){r=0;if(++n>=this.rows){n--;if(++a>this.ybase){a=this.ybase;break}}continue}break}this.x=r,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="^"||t==="0"){var s=this.x;if(t==="0")this.x=0;else if(t==="^"){var l=this.lines[this.ydisp+this.y],r=0;while(r" ")break;r++}r>=this.cols&&(r=this.cols-1),this.x=r}this.visualMode?this.selectText(s,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="$"){var s=this.x,l=this.lines[this.ydisp+this.y],r=this.cols-1;while(r>=0){if(l[r][1]>" "){this.visualMode&&r0&&a--):t==="}"&&n>=this.rows&&(n--,a" "){p===-1&&(p=0),c=!0;break}if(d===this.cols-1){p===-1?p=1:p===0?h=!0:p===1&&c&&(h=!0);break}}if(h)break;if(t==="{"){n--;if(n<0){n++;if(!(a>0))break;a--}}else if(t==="}"){n++;if(n>=this.rows){n--;if(!(a=this.cols)break;if(o[f+h][1]!==n[h])break;if(o[f+h][1]===n[h]&&h===n.length-1){u=!0;break}}if(u)break;f+=h+1}if(u)break;f=0;if(!p){l++;if(l>this.ybase+this.rows-1){if(a)break;a=!0,l=0}}else{l--;if(l<0){if(a)break;a=!0,l=this.ybase+this.rows-1}}}if(u){l-this.ybase<0?(c=l,l=0,c>this.ybase&&(l=c-this.ybase,c=this.ybase)):(c=this.ybase,l-=this.ybase),this.x=f,this.y=l,this.scrollDisp(-this.ydisp+c),this.visualMode&&this.selectText(r,this.x,i+s,this.ydisp+this.y);return}this.refresh(0,this.rows-1);return}if(t==="\b"||t===""){if(this.entry.length===0)return;var d=this.ydisp+this.rows-1;this.entry=this.entry.slice(0,-1);var h=this.entryPrefix.length+this.entry.length;this.lines[d][h]=[this.lines[d][h][0]," "],this.x--,this.refresh(this.rows-1,this.rows-1),this.refresh(this.y,this.y);return}if(t.length===1&&t>=" "&&t<="~"){var d=this.ydisp+this.rows-1;this.entry+=t;var h=this.entryPrefix.length+this.entry.length-1;this.lines[d][h]=[this.defAttr&-512|4,t],this.x++,this.refresh(this.rows-1,this.rows-1),this.refresh(this.y,this.y);return}return!1},h.charsets={},h.charsets.SCLD={"`":"◆",a:"▒",b:" ",c:"\f",d:"\r",e:"\n",f:"°",g:"±",h:"␤",i:" ",j:"┘",k:"┐",l:"┌",m:"└",n:"┼",o:"⎺",p:"⎻",q:"─",r:"⎼",s:"⎽",t:"├",u:"┤",v:"┴",w:"┬",x:"│",y:"≤",z:"≥","{":"π","|":"≠","}":"£","~":"·"},h.charsets.UK=null,h.charsets.US=null,h.charsets.Dutch=null,h.charsets.Finnish=null,h.charsets.French=null,h.charsets.FrenchCanadian=null,h.charsets.German=null,h.charsets.Italian=null,h.charsets.NorwegianDanish=null,h.charsets.Spanish=null,h.charsets.Swedish=null,h.charsets.Swiss=null,h.charsets.ISOLatin=null;var y=this.String,b=this.setTimeout,w=this.setInterval;x._cache={},x.distance=function(e,t,n,r,i,s){return Math.pow(30*(e-r),2)+Math.pow(59*(t-i),2)+Math.pow(11*(n-s),2)},h.EventEmitter=n,h.Stream=r,h.inherits=m,h.on=p,h.off=d,h.cancel=v,typeof module!="undefined"?module.exports=h:this.Terminal=h}).call(function(){return this||(typeof window!="undefined"?window:global)}());